#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/5/20  20:14
# @Author: 余浪人
# @email: yulangren520@gmail.com

from apps.client import client_bp
from flask import render_template, request,url_for
from apps.lib.public import get_date_str, page_auto
from apps.models.document_model import Document_modle, db
from apps.models.column_model import Column_model
from apps.models.system_model import Blogroll_Model, System_Model
from apps.lib.public import check_obj


@client_bp.route('/', endpoint='client_index', methods=['GET'])
def index():
    data = {
        'column': Column_model.query.order_by(db.asc(Column_model.column_order)).filter(Column_model.status == 1).all(),  # 栏目
        'today': Document_modle.query.filter(Document_modle.create_time == get_date_str()).count(),
        "index_doc": Document_modle.query.order_by(db.desc(Document_modle.is_index_stick)).order_by(
            db.desc(Document_modle.id)).filter(Document_modle.is_pass == 1).all(),  # 首页翻页
        "index_rec": Document_modle.query.order_by(db.desc(Document_modle.id)).filter(
            Document_modle.is_recommend == 1, Document_modle.is_pass == 1).limit(11),  # 推荐
        "index_host": Document_modle.query.order_by(db.desc(Document_modle.clicks)).filter(
            Document_modle.is_pass == 1).limit(10),  # 热门
        'blog_url': Blogroll_Model.query.order_by(db.asc(Blogroll_Model.link_sort)).filter(
            Blogroll_Model.status == 1).all(),  # 友情链接
        'webapp': System_Model.query.first(),

    }
    return render_template('client/index.html', data_s=data)


@client_bp.route('/article_list/<string:uid>', endpoint='article_list', methods=['GET'])
def list(uid):
    col = Column_model.query.filter_by(uuid=uid).first()
    if not check_obj(col):
        data_s, page = page_auto(Document_modle.query.order_by(db.desc(Document_modle.is_list_stick)).order_by(
            db.desc(Document_modle.id)).filter(Document_modle.is_pass == 1, Document_modle.superior == col.id))
        data = {
            'column': Column_model.query.order_by(db.asc(Column_model.column_order)).filter(Column_model.status == 1).all(),  # 栏目
            'webapp': System_Model.query.first(),
            'current_column': col,
            "doc_list": data_s,
            "page":page,
            "host": Document_modle.query.order_by(db.desc(Document_modle.clicks),Document_modle.superior==col.id).filter(
                Document_modle.is_pass == 1).limit(10),
            "new_list": Document_modle.query.order_by(db.desc(Document_modle.id),
                                                  Document_modle.superior == col.id).filter(
                Document_modle.is_pass == 1).limit(9),
            "random_list": Document_modle.query.order_by(Document_modle.superior == col.id).filter(
                Document_modle.is_pass == 1).limit(10)
        }
        return render_template('client/list.html', data_s=data,url = url_for('client_bp.article_list',uid=uid) )


@client_bp.route('/article/<string:uid>',endpoint='article',methods=['GET'])
def article(uid):
    obj = Document_modle.query.filter_by(uuid=uid).first()
    if not check_obj(obj):
        obj.clicks=obj.clicks+1  # 更新点击量
        data = {
            'column': Column_model.query.order_by(db.asc(Column_model.column_order)).filter(Column_model.status == 1).all(),
            'webapp': System_Model.query.first(),
            "article":obj,
            "host": Document_modle.query.order_by(db.desc(Document_modle.clicks), Document_modle.superior == obj.superior).filter(
                Document_modle.is_pass == 1).limit(10),
            "new_list": Document_modle.query.order_by(db.desc(Document_modle.id),
                                                      Document_modle.superior == obj.superior).filter(
                Document_modle.is_pass == 1).limit(9),
            "random_list": Document_modle.query.order_by(Document_modle.superior == obj.superior).filter(
                Document_modle.is_pass == 1).limit(10)
        }
        db.session.commit()
        return render_template('client/article.html',data_s=data)